package com.apkicon.parser;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * AndroidManifest.xml解析器
 * 按照Android原生原理解析应用图标引用
 */
public class AndroidManifestParser {
    
    /**
     * 解析AndroidManifest.xml获取应用图标信息
     */
    public static AppIconInfo parseAppIcon(ZipFile apkFile) throws Exception {
        // 读取AndroidManifest.xml
        ZipEntry manifestEntry = apkFile.getEntry("AndroidManifest.xml");
        if (manifestEntry == null) {
            throw new Exception("AndroidManifest.xml not found in APK");
        }
        
        try (InputStream manifestStream = apkFile.getInputStream(manifestEntry)) {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(manifestStream);
            
            Element root = document.getDocumentElement();
            NodeList applicationNodes = root.getElementsByTagName("application");
            
            if (applicationNodes.getLength() == 0) {
                throw new Exception("No application tag found in AndroidManifest.xml");
            }
            
            Element application = (Element) applicationNodes.item(0);
            
            // 解析应用图标
            String iconRef = application.getAttribute("android:icon");
            if (iconRef == null || iconRef.isEmpty()) {
                throw new Exception("No android:icon attribute found in application tag");
            }
            
            System.out.println("🔍 AndroidManifest.xml中的图标引用: " + iconRef);
            
            // 解析Adaptive Icon（如果存在）
            String adaptiveIconRef = application.getAttribute("android:adaptiveIcon");
            boolean hasAdaptiveIcon = adaptiveIconRef != null && !adaptiveIconRef.isEmpty();
            
            if (hasAdaptiveIcon) {
                System.out.println("🔍 发现Adaptive Icon引用: " + adaptiveIconRef);
            }
            
            return new AppIconInfo(iconRef, adaptiveIconRef, hasAdaptiveIcon);
        }
    }
    
    /**
     * 应用图标信息
     */
    public static class AppIconInfo {
        private final String iconRef;
        private final String adaptiveIconRef;
        private final boolean hasAdaptiveIcon;
        
        public AppIconInfo(String iconRef, String adaptiveIconRef, boolean hasAdaptiveIcon) {
            this.iconRef = iconRef;
            this.adaptiveIconRef = adaptiveIconRef;
            this.hasAdaptiveIcon = hasAdaptiveIcon;
        }
        
        public String getIconRef() {
            return iconRef;
        }
        
        public String getAdaptiveIconRef() {
            return adaptiveIconRef;
        }
        
        public boolean hasAdaptiveIcon() {
            return hasAdaptiveIcon;
        }
        
        /**
         * 从资源引用中提取资源名称
         * 例如: @mipmap/ic_launcher -> ic_launcher
         */
        public String getResourceName() {
            if (iconRef.startsWith("@")) {
                return iconRef.substring(iconRef.lastIndexOf("/") + 1);
            }
            return iconRef;
        }
        
        /**
         * 从资源引用中提取资源类型
         * 例如: @mipmap/ic_launcher -> mipmap
         */
        public String getResourceType() {
            if (iconRef.startsWith("@")) {
                String[] parts = iconRef.substring(1).split("/");
                return parts.length > 0 ? parts[0] : "drawable";
            }
            return "drawable";
        }
    }
}
